#include "widget.h"
#include "rasterwidget.h"

#include <QSize>
#include <QFormLayout>
#include <QGroupBox>


#define UI_SOURCE_UIFILE   1
#define UI_SOURCE_CODE     2
#define UI_SOURCE          UI_SOURCE_CODE


#if UI_SOURCE == UI_SOURCE_UIFILE
#include "./ui_widget.h"
#endif


Widget::Widget(QWidget *parent)
    : QWidget(parent)
#if UI_SOURCE == UI_SOURCE_UIFILE
    , ui(new Ui::Widget)
#endif
{
    setWindowTitle("My Render");
#if UI_SOURCE == UI_SOURCE_UIFILE
    ui->setupUi(this);
    QSize size = ui->widgetMain->size();
    ui->labelSizeInfo->setText(QString("%1x%2").arg(size.width()).arg(size.height()));
    connect(ui->widgetMain, &RasterWidget::sizeChanged, this, &Widget::widgetMain_sizeChanged);

#if 0
    connect(ui->btnRender, SIGNAL(clicked(bool)), this, SLOT(on_btnRender_clicked(bool)));
#endif

#elif UI_SOURCE == UI_SOURCE_CODE
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // top
    QHBoxLayout *topLyout = new QHBoxLayout();

    // left
    widgetMain = new RasterWidget(this);
    topLyout->addWidget(widgetMain, 1);

    //right
    QGroupBox *groupBox = new QGroupBox("参数");
    QVBoxLayout *rightLayout = new QVBoxLayout(groupBox);

    // first section: form layout
    QFormLayout *formLayout = new QFormLayout();
    QHBoxLayout *row1Layout = new QHBoxLayout();
    QLabel *lbEye   = new QLabel("eye");
    auto eyePos = widgetMain->eyePos();
    QStringList placeholderStr = { "X", "Y", "Z" };
    assert(kEditCount == placeholderStr.size());
    for (int i = 0; i < kEditCount; i++) {
        editEye[i] = new QLineEdit();
        editEye[i]->setFixedWidth(60);
        editEye[i]->setText(QString::number(eyePos[i], 'g', 4));
        editEye[i]->setPlaceholderText(placeholderStr.at(i));
        row1Layout->addWidget(editEye[i]);
    }
    formLayout->addRow(lbEye, row1Layout);

    QHBoxLayout *row2Layout = new QHBoxLayout();
    QLabel *lbLight = new QLabel("light");
    auto lightDir = widgetMain->lightDir();
    for (int i = 0; i < kEditCount; i++) {
        editLight[i] = new QLineEdit();
        editLight[i]->setFixedWidth(60);
        editLight[i]->setText(QString::number(lightDir[i], 'g', 4));
        editLight[i]->setPlaceholderText(placeholderStr.at(i));
        row2Layout->addWidget(editLight[i]);
    }
    formLayout->addRow(lbLight, row2Layout);

    rightLayout->addLayout(formLayout);

    // second section: spacer
    rightLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));

    // third section: button
    btnRender = new QPushButton("渲染", groupBox);
    btnRender->setStyleSheet(
        "QPushButton {"
        " backgroud-color: #4CAF50;"
        " color: white"
        " border: none"
        " padding: 8px"
        " font-size: 14px"
        " min-width: 100px"
        "}"
        "QPushButton: hover { background-color: #45A049; }"
        );
    rightLayout->addWidget(btnRender);

    topLyout->addWidget(groupBox);

    // bottom
    lbSizeInfo = new QLabel("width x height", this);
    QSize size = widgetMain->size();
    lbSizeInfo->setText(QString("%1x%2").arg(size.width()).arg(size.height()));

    mainLayout->addLayout(topLyout, 1);
    mainLayout->addWidget(lbSizeInfo);

    connect(btnRender, SIGNAL(clicked(bool)), this, SLOT(on_btnRender_clicked(bool)));
#endif
}

Widget::~Widget()
{
#if UI_SOURCE == UI_SOURCE_UIFILE
    delete ui;
#endif
}

void Widget::on_btnRender_clicked(bool checked)
{
    if (!checked) { // released
        qDebug() << "disable btnRender";

        // eye pos
        bool res = false;
        std::vector<float> eyeVec(kEditCount, 0);
        for (int i = 0; i < kEditCount; i++) {
            float d = editEye[i]->text().toFloat(&res);
            if (!res) {
                qDebug() << "Please input valid float number";
            }
            eyeVec[i] = d;
        }
        widgetMain->setEyePos(eyeVec);

        // light dir
        std::vector<float> lightVec(kEditCount, 0);
        for (int i = 0; i < kEditCount; i++) {
            float d = editLight[i]->text().toFloat(&res);
            if (!res) {
                qDebug() << "Please input valid float number";
                break;
            }
            lightVec[i] = d;
        }
        widgetMain->setLightDir(lightVec);

        // button
        btnRender->setEnabled(false);
        std::function<void(void)> callback = [&](){
            btnRender->setEnabled(true);
            QSize size = widgetMain->size();
            lbSizeInfo->setText(QString("%1x%2").arg(size.width()).arg(size.height()));
            qDebug() << "enable btnRender";
        };

        // raster widget
        widgetMain->render(callback);
    }
    qDebug() << "btnRender clicked, status: " << checked;
}

void Widget::widgetMain_sizeChanged()
{
    qDebug() << "widgetMain_sizeChanged";
    QSize size = widgetMain->size();
    lbSizeInfo->setText(QString("%1x%2").arg(size.width()).arg(size.height()));
}

